<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AbonProjetManager
 *
 * @author camara
 */
require_once '../model/AbonProjet.php';

class AbonProjetManager {

    //put your code here
    private $bdd; // Instance de PDO

    public function __construct($db) {
        $this->setDb($db);
    }

    public function add(AbonProjet $abonProjet) {

        $q = $this->bdd->prepare('INSERT INTO ABON_PROJET SET idUser = :idUser, idProjet = :idProjet, libelle = :libelle, commentaires = :commentaires');
        $q->bindValue(':idUser', $abonProjet->getIdUser());
        $q->bindValue(':idProjet', $abonProjet->getIdProjet());
        $q->bindValue(':libelle', $abonProjet->getLibelle());
        $q->bindValue(':commentaires', $abonProjet->getCommentaire());

        $q->execute();
        //print_r($q->errorInfo());
    }

    public function getListAbonProjet($id) {
        $listAbonProjet = array();
        $req = $this->bdd->prepare('SELECT * FROM ABON_PROJET WHERE idUser='.$id);
        $req->execute();
        while ($donnees = $req->fetch()) {
            $abonProjet = new AbonProjet($donnees["idProjet"], $donnees["idUser"], $donnees["libelle"], $donnees["commentaires"]);
            array_push($listAbonProjet, $abonProjet);
        }
        return $listAbonProjet;
    }

    public function setDb($db) {
        $this->bdd = $db;
    }

    public function getDb() {
        return $this->bdd;
    }
    
    public function getCount ($idUser) {
        $requete ="SELECT COUNT(idProjet) FROM ABON_PROJET WHERE idUser=$idUser";
        $req = $this->bdd->prepare($requete);
        $req->execute();
        $donnees = $req->fetch();
        $var= $donnees[0];
        //var_dump($donnees);
        return $var;
    }

}
